package com.wecommodule.bpm.ext.framework.config.support;

import okhttp3.ConnectionPool;
import okhttp3.ConnectionSpec;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.concurrent.TimeUnit;


public class OkHttpConfigSupport {
    /**
     * 生成一个OkHttp
     *
     * @param connectionSpecs        支持的连接协议
     * @param httpConfigProperties   连接配置
     * @param followRedirects        是否允许重定向
     * @param additionalInterceptors 添加拦截器
     * @return  OkHttClient.Builder
     */
    @SuppressWarnings("FeatureEnvy")
    @NotNull
    public static OkHttpClient.Builder defaultBuilder(@NotNull List<ConnectionSpec> connectionSpecs,
                                                       @NotNull HttpConfigProperties httpConfigProperties,
                                                       boolean followRedirects,
                                                       @Nullable Interceptor[] additionalInterceptors) {
        httpConfigProperties.guard();
        ConnectionPool connectionPool = new ConnectionPool(httpConfigProperties.getMaxIdle(),
                httpConfigProperties.getKeepAliveSeconds(), TimeUnit.SECONDS);
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectionSpecs(connectionSpecs)
                .connectionPool(connectionPool)
                .eventListener(new FailureEventListener());
        if (httpConfigProperties.getCallTimeoutMS() > 0) {
            builder.callTimeout(httpConfigProperties.getCallTimeoutMS(), TimeUnit.MILLISECONDS);
        }
        if (httpConfigProperties.getConnectTimeoutMS() > 0) {
            builder.connectTimeout(httpConfigProperties.getConnectTimeoutMS(), TimeUnit.MILLISECONDS);
        }
        if (httpConfigProperties.getWriteTimeoutMS() > 0) {
            builder.writeTimeout(httpConfigProperties.getWriteTimeoutMS(), TimeUnit.MILLISECONDS);
        }
        if (httpConfigProperties.getReadTimeoutMS() > 0) {
            builder.readTimeout(httpConfigProperties.getReadTimeoutMS(), TimeUnit.MILLISECONDS);
        }
        if (followRedirects) {
            builder.followRedirects(true).followSslRedirects(true);
        } else {
            builder.followRedirects(false).followSslRedirects(false);
        }
        addInterceptors(builder, additionalInterceptors);
        return builder;
    }

    /**
     * 添加OkHttp拦截器
     *
     * @param builder okhttp
     * @param additionalInterceptors 连接器列表
     */
    private static void addInterceptors(@NotNull OkHttpClient.Builder builder,
                                        @Nullable Interceptor[] additionalInterceptors) {
        if (additionalInterceptors != null) {
            for (Interceptor interceptor : additionalInterceptors) {
                if (interceptor != null) {
                    builder.addInterceptor(interceptor);
                }
            }
        }
    }
}
